<?php

namespace app\modules\Agreement\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Agreement\models\Agreement;

/**
 * AgreementSearch represents the model behind the search form of `app\modules\Agreement\models\Agreement`.
 */
class AgreementSearch extends Agreement
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'customer_id', 'type', 'order_type', 'user_id', 'status'], 'integer'],
            [['code', 'sale_currency', 'payment_type', 'payment_date', 'warranty', 'exemption', 'trade_company', 'trade_contract', 'invoice_no', 'trade_main', 'send_date', 'install_date', 'remarks', 'created_at', 'updated_at'], 'safe'],
            [['sale_price'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Agreement::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'customer_id' => $this->customer_id,
            'type' => $this->type,
            'order_type' => $this->order_type,
            'sale_price' => $this->sale_price,
            'send_date' => $this->send_date,
            'install_date' => $this->install_date,
            'user_id' => $this->user_id,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'sale_currency', $this->sale_currency])
            ->andFilterWhere(['like', 'payment_type', $this->payment_type])
            ->andFilterWhere(['like', 'payment_date', $this->payment_date])
            ->andFilterWhere(['like', 'warranty', $this->warranty])
            ->andFilterWhere(['like', 'exemption', $this->exemption])
            ->andFilterWhere(['like', 'trade_company', $this->trade_company])
            ->andFilterWhere(['like', 'trade_contract', $this->trade_contract])
            ->andFilterWhere(['like', 'invoice_no', $this->invoice_no])
            ->andFilterWhere(['like', 'trade_main', $this->trade_main])
            ->andFilterWhere(['like', 'remarks', $this->remarks]);

        return $dataProvider;
    }
}
